package com.gitee.sop.layui.adminserver.module.background.admin.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.sop.layui.adminserver.entrance.bo.ServiceContext;
import com.gitee.sop.layui.adminserver.module.background.admin.dto.SysUserDTO;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysUser;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysUserService;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import com.gitee.sop.layui.adminserver.module.base.error.ServiceInterruptionException;
import com.gitee.sop.layui.adminserver.module.base.util.AssertUtil;
import com.gitee.sop.layui.adminserver.module.base.util.ObjectUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * com.zch.admin.base.entity.SysUser模型http接口处理
 *
 * @author: admin
 * @date: 2019-11-13
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/admin/user")
public class SysUserRestController {
    @Lazy
    @Resource
    private SysUserService baseService;

    @PostMapping(value = {"/add"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result add(SysUserDTO query) {
        SysUser sysUser = new SysUser();
        ObjectUtil.switchAllFieldValue(sysUser, query);
        baseService.add(sysUser);
        return Result.success();
    }

    @PostMapping(value = {"/delete/{uuid}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result deleteById(@PathVariable(value = "uuid") String uuid) {
        SysUser sysUser = ServiceContext.me().checkedUserAndGet(SysUser.class);
        AssertUtil.isFalse(sysUser.getUuid().equals(uuid), ServiceInterruptionException.fail(400, "抹杀自己，不想活了?"));

        baseService.deleteByUuid(uuid);
        return Result.success();
    }

    @PostMapping(value = {"/update/{uuid}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result updateById(@PathVariable(value = "uuid") String uuid, SysUserDTO query) {
        SysUser modify = new SysUser();
        ObjectUtil.switchAllFieldValue(modify, query);
        modify.setUuid(uuid);

        baseService.updateByUuid(modify);
        return Result.success();
    }

    @PostMapping(value = {"/set/password"})
    public Result setPassword(@RequestParam(value = "oldPassword") String oldPassword, @RequestParam(value = "password") String password) {
        baseService.setPassword(oldPassword, password);
        return Result.success();
    }

    @RequestMapping(value = {"/search/page"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchPage(@RequestParam(required = false) Integer current, @RequestParam(required = false) Integer size,
                             @RequestParam(required = false) String name, @RequestParam(required = false) String username) {
        IPage<SysUser> page = baseService.getPageByUserUuid(current, size, name, username);
        return Result.success(page);
    }

    @RequestMapping(value = {"/search/{uuid}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchOneById(@PathVariable(value = "uuid") String uuid) {
        return Result.success(baseService.getOneByUuid(uuid));
    }
}
